package com.jp.vn.CustomAdapter;

import java.io.IOException;
import java.util.ArrayList;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.graphics.Color;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jp.vn.main.R;
import com.jp.vn.Object.ChaoHoi;

public class ChaoHoiAdapter extends BaseExpandableListAdapter {
	
	Context context;
	ArrayList<ChaoHoi> arrayChaoHoi;
	LayoutInflater inflater;
	Typeface type,type1;
	MediaPlayer mediaPlayer = null;
	
	public ChaoHoiAdapter(Context context, ArrayList<ChaoHoi> arrayChaoHoi) {
		this.context = context;
		this.arrayChaoHoi = arrayChaoHoi;
		inflater =  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		type = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular.ttf");
		type1 = Typeface.createFromAsset(context.getAssets(), "fonts/tekton.ttf");
	}
	
	@Override
	public int getGroupCount() {
		return arrayChaoHoi.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return 1;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {
		View v = convertView;

		if (v == null) {
			v = inflater.inflate(R.layout.group_chaohoi, parent, false);
		}

		v.setMinimumHeight((int) context.getResources().getDimension(
				R.dimen.group_chaohoi));
		
		TextView tfChaoHoiVn = (TextView)v.findViewById(R.id.txtName);
		tfChaoHoiVn.setText(arrayChaoHoi.get(groupPosition).getVnChaoHoi());
		tfChaoHoiVn.setTypeface(type);

		v.setBackgroundColor(groupPosition % 2 == 0 ? Color
				.parseColor("#f75f8c") : Color.parseColor("#f979a0"));
		
		if (isExpanded) {
			try {
				stopPlaying();
				playSound(String.valueOf(groupPosition));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return v;
	}
	
	public class ViewHolder{
		TextView tfChaoHoiJp, tfChaoHoiLatin;
		ImageView btnAudio;
	}
	
	@Override
	public View getChildView(final int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {
		View v = convertView;
		ViewHolder holder;
		
		if(v == null){
			holder = new ViewHolder();
			v = inflater.inflate(R.layout.item_chaohoi, parent,false);
			holder.tfChaoHoiJp = (TextView)v.findViewById(R.id.tfChaoHoiJp);
			holder.tfChaoHoiLatin = (TextView)v.findViewById(R.id.tfChaoHoiRm);
			holder.btnAudio = (ImageView) v.findViewById(R.id.btnAudio);
			v.setTag(holder);
		} else{
			holder = (ViewHolder)v.getTag();
		}
		
		holder.tfChaoHoiJp.setText(arrayChaoHoi.get(groupPosition).getJpChaoHoi());
		holder.tfChaoHoiLatin.setText(arrayChaoHoi.get(groupPosition).getRomanjiChaoHoi());
		holder.btnAudio.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				try {
					stopPlaying();
					playSound(String.valueOf(groupPosition));
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
		
		v.setMinimumHeight((int) context.getResources().getDimension(R.dimen.item_chaohoi));
		holder.tfChaoHoiJp.setTypeface(type1);
		v.setBackgroundColor(Color.parseColor("#fbe0e8"));
		
		return v;
	}
	
	@Override
	public Object getGroup(int arg0) {
		return null;
	}
	
	@Override
	public long getGroupId(int arg0) {
		return 0;
	}
	
	@Override
	public Object getChild(int arg0, int arg1) {
		return null;
	}

	@Override
	public long getChildId(int arg0, int arg1) {
		return 0;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		return false;
	}
	
	public void playSound(String fileName) throws IOException {
		AssetFileDescriptor afd = context.getAssets().openFd(
				"chaohoi/" + fileName + ".ogg");
		mediaPlayer = new MediaPlayer();
		mediaPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());
		mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

			@Override
			public void onPrepared(MediaPlayer mp) {
				mp.start();
			}
		});

		mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

					@Override
					public void onCompletion(MediaPlayer mp) {
						mp.release();
						mediaPlayer = null;
					}
				});

		mediaPlayer.prepareAsync();
	}
	
	public void stopPlaying(){
		if (mediaPlayer != null) {
			mediaPlayer.stop();
			mediaPlayer.release();
			mediaPlayer = null;
		}
	}

}
